home *** CD-ROM | disk | FTP | other *** search
/ PC Media 2 / PC MEDIA CD02.iso / share / prog / tpsorts / qsort.inc < prev    next >
Encoding:
Text File  |  1986-03-12  |  2.3 KB  |  70 lines

  1. {
  2.  
  3. QSORT.INC
  4.  
  5. (c) 1985 Renaissance Software
  6.  
  7.  
  8.                               Quicksort Algorithm
  9. }
  10.  
  11. procedure partition( min,max   :integer;       {  min=lower bounds        }
  12.                      var pl    :integer;       {  max=upper bounds        }
  13.                      var table :sortarray);    {  table=data to sort      }
  14.  
  15.  
  16.                                                { if you want to know      }
  17. var                                            { what's going on in       }
  18.    j      :integer;                            { this block then you'll   }
  19.    parted :boolean;                            { have to shell out the    }
  20.    temp   :arrayptr;                           { 35 bucks for Kunth vol.3 }
  21.  
  22. begin {partition}
  23.      j:=min+1;
  24.      pl:=max;
  25.      parted:=false;
  26.      repeat
  27.            while not ((table[j]^>table[min]^) or (j>=pl))
  28.                  do j:=j+1;
  29.            while not (table[pl]^<=table[min]^)
  30.                  do pl:=pl-1;
  31.            if  (pl<=j) then begin
  32.                temp:=table[min];
  33.                 table[min]:=table[pl];
  34.                 table[pl]:=temp;
  35.                 parted:=true;
  36.            end
  37.            else begin
  38.                 temp:=table[j];
  39.                 table[j]:=table[pl];
  40.                 table[pl]:=temp;
  41.                 j:=j+1;
  42.                 pl:=pl-1;
  43.            end;
  44.      until parted;
  45. end; {partition}
  46.  
  47. {
  48.  ---------------------------------------------------------------------------
  49.                                                                             }
  50.                                                {  min=lower bounds  }
  51. procedure qsort(    min,max    :integer;       { max=upper bounds   }
  52.                  var table     :sortarray);    { table=data to sort }
  53.  
  54. var
  55.    pl:integer;
  56.  
  57.  
  58. begin  {qsort}
  59.      if max>min then begin
  60.         partition(min,max,pl,table);
  61.         qsort(min,pl-1,table);                  { qsort is recursive }
  62.         qsort(pl+1,max,table);                  { comments aren't    }
  63.      end;
  64. end; {qsort}
  65.  
  66. {
  67.  ---------------------------------------------------------------------------
  68.                                                                             }
  69.  
  70.